<?php
namespace Admin\Controller;
use Think\Controller;
class FileController extends Controller {
    protected $fileModel;

    public function __construct(){
        parent::__construct();
        $this->fileModel=D('file');
        $this->verification();
    }
    //文件列表模板
    public function file(){
        if(isset($_GET['cid'])) {
            $this->queryfile();
        }else{
            $this->filelist();
        }
        $this->filecate(1);
        $this->albumlist();
        $this->artcate(2);
        $this->display();
	}

	//文件回收站模板
    public function filebin(){
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->binlist();
        $this->display();
    }

    //验证是否登陆
    protected function verification(){
        $userModel=D('user');
        $userinfo=$userModel->where(array('account'=>cookie('account')))->find();
        if(!che() || !$userinfo){
            $url=U('User/login');
            echo "<script>alert('对不起，请您先登陆。');location.href='$url';</script>";
        }
    }

    //获取文件栏目
    protected function filecate($pid){
        $cateModel=D('cate');
        $filecate=$cateModel->gettree($pid);
        $this->assign('filecate',$filecate);
    }

    //获取文章栏目
    protected function artcate($pid){
        $cateModel=D('cate');
        $artcate=$cateModel->gettree($pid);
        $this->assign('artcate',$artcate);
    }

    //获取搜索栏的相册栏目
    protected function albumlist(){
        $albumModel=D('album');
        $albumlist=$albumModel->select();
        $this->assign('albumlist',$albumlist);
    }

    //第一页与最后一页的提示
    protected function page_tips(){
        $file_all=$this->fileModel->where('is_del=0')->select();
        $file_count = count($file_all);
        if(I('get.start')<0){
            echo "<script>alert('已经第一页了');window.location.href=document.referrer;</script>";
        }else if(I('get.start')>$file_count){
            echo "<script>alert('已经是最后一页了');location.href='javascript:history.go(-1)';</script>";
        }
    }

    //文件列表
    protected function filelist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $file_list=$this->fileModel->where('is_del=0')->order('fid desc')->limit($start . ',' . $pagecount)->select();
        $file_all=$this->fileModel->where('is_del=0')->select();
        if($file_list === false || $file_all === false) {
            $this->error("获取文件列表失败");
        }else {
            $file_rowcount = count($file_list);
            $file_count = count($file_all);
        }
        //$this->assign('file_rowcount',$file_rowcount);
        $this->assign('file_count',$file_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('file_list',$file_list);
        //$this->assign('file_all',$file_all);
    }

    //文件栏目选择查询
    protected function queryfile(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $query_file=$this->fileModel->where('is_del=0 and cid=' . I('cid'))->order('fid desc')->limit($start . ',' . $pagecount)->select();
        $query_all=$this->fileModel->where('is_del=0 and cid=' . I('cid'))->select();
        if($query_file === false || $query_all === false) {
            $this->error("获取栏目文件列表失败");
        }else {
            //$query_rowcount = count($query_file);
            $query_count = count($query_all);
        }
        //$this->assign('file_rowcount',$query_rowcount);
        $this->assign('file_count',$query_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('file_list',$query_file);
        //$this->assign('file_all',$query_all);
    }

    //文件回收站列表
    protected function binlist(){
        $this->page_tips();
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $bin_list=$this->fileModel->where('is_del=1')->order('fid desc')->limit($start . ',' . $pagecount)->select();
        $bin_all=$this->fileModel->where('is_del=1')->select();
        if($bin_list === false || $bin_all === false) {
            $this->error("获取文件回收站列表失败");
        }else {
            $bin_rowcount = count($bin_list);
            //echo "查询到的结果行数：" . $rowcount . "<br/>";
            $bin_count = count($bin_all);
            //echo "总行数：$count<br/>";
        }
        //$this->assign('bin_rowcount',$bin_rowcount);
        $this->assign('bin_count',$bin_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('bin_list',$bin_list);
        //$this->assign('bin_all',$bin_all);
    }

    //将文件放入回收站
    public function filedel(){
        if(I('get.fid')){
            $data['is_del']=1;
            $con['fid']=I('get.fid');
            if($this->fileModel->where($con)->save($data)){
                echo "<script>alert('成功放入回收站!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
            }
        }
    }

    //将文件从回收站中还原
    public function binres(){
        if(I('get.fid')){
            $data['is_del']=0;
            $con['fid']=I('get.fid');
            if($this->fileModel->where($con)->save($data)){
                echo "<script>alert('成功还原文件!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
            }
        }
    }

    //将文件从回收站中删除
    public function bindel(){
        if(I('get.fid')){
            if($this->fileModel->delete(I('get.fid'))){
                echo "<script>alert('删除成功!');location.href=document.referrer;</script>";
            }
        }
    }

}